package com.atguigu.flink.chapter6;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2020/12/22 9:30
 */
public class Flink04_Project_AdsClick {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> sourceStream = env.readTextFile("input/AdClickLog.csv");
        sourceStream
          .map(new MapFunction<String, AdsClickLog>() {
              @Override
              public AdsClickLog map(String line) throws Exception {
                  String[] datas = line.split(",");
                  return new AdsClickLog(Long.valueOf(datas[0]), Long.valueOf(datas[1]), datas[2], datas[3], Long.valueOf(datas[4]));

              }
          })
          .map(new MapFunction<AdsClickLog, Tuple2<Tuple2<String, Long>, Long>>() {

              @Override
              public Tuple2<Tuple2<String, Long>, Long> map(AdsClickLog adsLog) throws Exception {
                  return Tuple2.of(Tuple2.of(adsLog.getProvince(), adsLog.getAdId()), 1L);
              }
          })
          .keyBy(new KeySelector<Tuple2<Tuple2<String, Long>, Long>, Tuple2<String, Long>>() {
              @Override
              public Tuple2<String, Long> getKey(Tuple2<Tuple2<String, Long>, Long> value) throws Exception {
                  return value.f0;
              }
          })
          .reduce(new ReduceFunction<Tuple2<Tuple2<String, Long>, Long>>() {
              @Override
              public Tuple2<Tuple2<String, Long>, Long> reduce(Tuple2<Tuple2<String, Long>, Long> value1,
                                                               Tuple2<Tuple2<String, Long>, Long> value2) throws Exception {
                  return Tuple2.of(value1.f0, value1.f1 + value2.f1);
              }
          })
          .print();

        env.execute();
    }
}
